//
//  UUAppIntroduceVC.m
//  Whistle
//
//  Created by ZhangAo on 2016/11/15.
//  Copyright © 2016年 BookSir. All rights reserved.
//

#import "UUAppIntroduceVC.h"
#import "UUIntroduceManager.h"
#import "FLAnimatedImageView.h"
#import "FLAnimatedImage.h"

@interface UUAppIntroduceVC ()

@property (nonatomic, strong) FLAnimatedImageView *adPictureView;
@property (nonatomic, strong) NSTimer *advertisementTimer;
@property (nonatomic, strong) UILabel *passLabel;
@property (nonatomic, strong) UIControl *passLabelContainer;
@property (nonatomic, assign) int countTime;
@property (nonatomic, assign) BOOL enableTimer;
@property (nonatomic, copy) void (^completeBlock)(void);

@end

#define ShowTime 3

@implementation UUAppIntroduceVC

+ (instancetype)initWithCompleteBlock:(void (^)(void))completeBlock {
    UUAppIntroduceVC *appIntroduceVC = [UUAppIntroduceVC new];
    appIntroduceVC.completeBlock = completeBlock;
    return appIntroduceVC;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.enableTimer = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSData *imageData = [GetIntroduceManager() fetchLaunchImage];
    if (imageData == nil) {
//        self.adPictureView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%.fx%.f",SCREEN_WIDTH,SCREEN_HEIGHT]];
//        self.adPictureView.contentMode = UIViewContentModeScaleToFill;
    } else {
        self.adPictureView.contentMode = UIViewContentModeScaleAspectFill;
        if (imageData && [imageData imageFormat] == DKImageFormatGIF) {
            self.adPictureView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
        } else {
            self.adPictureView.image = [UIImage imageWithData:imageData];
        }
    }
    
    if (self.adPictureView.image == nil) {
        [self pass];
    } else {
        [self adImageAnimation];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
 
    self.enableTimer = NO;
    [self invalidateTimer];
}

- (void)setupUI {
    self.adPictureView = [FLAnimatedImageView new];
    self.adPictureView.clipsToBounds = YES;
    self.adPictureView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.adPictureView];
    [self.adPictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-120);
    }];
    
    self.passLabel = [[UILabel alloc] init];
    self.passLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:15];
    self.passLabel.textColor = [UIColor whiteColor];
    self.passLabel.backgroundColor = [UIColor clearColor];
    self.passLabel.textAlignment = NSTextAlignmentCenter;
    self.passLabel.layer.cornerRadius = 4;
    self.passLabel.layer.masksToBounds = YES;
    [self passText:ShowTime];
    [self.view addSubview:self.passLabel];
    [self.passLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.adPictureView.mas_top).offset(25);
        make.right.equalTo(self.view).offset(-15);
        make.height.equalTo(@30);
        make.width.equalTo(@60);
    }];
    
    self.passLabelContainer = [UIControl new];
    [self.passLabelContainer addTarget:self action:@selector(pass) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.passLabelContainer];
    [self.passLabelContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.passLabel);
        make.height.and.width.equalTo(self.passLabel).offset(30);
    }];
    
    self.adPictureView.alpha = 0.f;
}

// 隐藏StatusBar
- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)adImageAnimation {
    [UIView animateWithDuration:0.2 animations:^{
        self.adPictureView.alpha = 1.f;
        self.passLabel.backgroundColor = RGB_COLOR_ALPHA(187, 187, 187, 0.4);
    } completion:^(BOOL finished) {
        [self startTimer];
    }];
}

- (void)startTimer {
    if (self.enableTimer) {
        _countTime = ShowTime;
        [self invalidateTimer];
        _advertisementTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    }
}

- (void)countDown {
    --_countTime;
    [self passText:_countTime];
    if (_countTime == 0) {
        [self pass];
    }
}

- (void)pass {
    [self invalidateTimer];
    _advertisementTimer = nil;
    
    self.completeBlock();
}

- (void)passText:(int)countTime {
    self.passLabel.text = [NSString stringWithFormat:@"跳过%d",countTime];
}

- (void)invalidateTimer {
    [_advertisementTimer invalidate];
    _advertisementTimer = nil;
}

@end
